home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / sectools / SRS / client / src / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-12  |  11.2 KB  |  480 lines

  1. /* FIX - use K&R style C syntax for compatibility w/ old stuff */
  2.  
  3. #include "headers.h" /* includes function declarations, macros, etc. */
  4.  
  5.  
  6. /* ----- global data ----- */
  7. int sockfd;               /* main fd to the server         */
  8.  
  9. int curlogfd = ERROR;     /* index into logs[]             */
  10. int curuser = ERROR;      /* index into usernames[]        */
  11.  
  12. int nullfd;               /* /dev/null, used for stdin     */
  13. int klogfd = ERROR;       /* /dev/klog fd                  */
  14.  
  15. int  dblogfd = 0;         /* fd for the debug log          */
  16. FILE *dblogfd1 = NULL;    /* same file but FILE *          */ 
  17.  
  18. int errlogfd = 0;         /* fd for the error log          */
  19. FILE *errlogfd1 = NULL;   /* same file but FILE *          */ 
  20.  
  21. FILE *confd = NULL;       /* syslog config file            */
  22. FILE *spoolfd = NULL;     /* where to spool to locally     */
  23.  
  24. int connected = 0;        /* are we connected to the serv? */
  25.  
  26. int subID;                /* our sub-ID sent by server     */
  27.  
  28. int spooling = 0;         /* are we currently spooling?    */            
  29. int didspool = 0;         /* were we previously spooling?  */
  30. pid_t spoolpid = ERROR;   /* pid of the spooling process   */
  31.  
  32. int pinging = 1;          /* are we pinging or not?        */
  33. volatile int streaming;   /* set to 1 hen we're streaming  */
  34.  
  35. int  shmID;               /* key ID for shared memory      */
  36. char *segptr;             /* points to shared mem segment  */
  37. char oldbuf[MAXSEGSIZE];  /* has old shared mem.. compare  */
  38.  
  39. volatile pid_t chpid;     /* child's pid (used to kill it) */
  40.  
  41. int locPort = ERROR;      /* local port to bind to         */
  42. int curport = ERROR;      /* current port to bind to       */
  43.  
  44. struct sockaddr_in laddr, daddr;
  45.  
  46. int useServ = ERROR;        /* server # in list to start w/ #0  */
  47. int curServ = ERROR;        /* index into the server structure  */
  48.  
  49. char SRSdir[MAXDNAMESIZE]; 
  50.  
  51. int gotInfoServ = 0; /* successfully connected to info server    */
  52.  
  53. struct passwd *pwd;
  54.  
  55. /* things for select() */
  56. int nfds, funix;
  57. fd_set readfds, unixm;
  58.  
  59. int prevmsg = ERROR; /* used to keep track of previous msg type */
  60.  
  61. int repcount = 0;
  62. long prevlogtime;
  63. char prevlogmsg[MAXLOGSIZE];
  64.  
  65. int prim = 1;   /* 1 == conn to prim serv, 0 == conn to sec serv */
  66. int silent = 0; /* 1 == don't output conn info */
  67. int errors = 0; /* used to determine if an error occured */
  68.  
  69. int start = 1;  /* 1 when first started */
  70. int done  = 0;  /* was quit() called?   */
  71.  
  72. int child = 0;  /* is the ping process up yet? */
  73.  
  74. int initing;     /* are we initializing? */
  75.  
  76. /* files the user can define.. */
  77. char *spoolFile, *dbFile, *errorFile;
  78.  
  79. #ifndef DEBUG
  80. volatile int debugging = 0; /* are we running in debug mode? */
  81. #else
  82. volatile int debugging = 1; /* are we running in debug mode? */
  83. #endif
  84.  
  85. volatile sig_atomic_t running = 1; /* we start running */
  86. volatile sig_atomic_t newData = 1; /* no new data yet  */
  87. volatile sig_atomic_t gotAlrm = 0; /* no alarm set yet */
  88.  
  89. jmp_buf dospool, infoconn, reconnect, dropconn, doquit, sigdropconn;
  90.  
  91. #ifndef NOSSL
  92. SSL *sslconn = NULL;
  93. SSL_CTX *sslctx = NULL;
  94. #endif
  95.  
  96. /* ----------------------- */
  97.  
  98. /* reports proper usage (local function only) */
  99. void usage(char *progname)
  100. {
  101.    (void)fprintf(stderr, "Usage: %s "
  102.                  "[-h] [-d] [-p port] [-e file] [-s file] [-l file]\n\n",
  103.                  progname);
  104.  
  105.    (void)fprintf(stderr, 
  106.          "-h        help (this)\n"
  107.          "-d        enable debugging (good idea to enable)\n"
  108.          "-p port   local port to bind to (must be between 512-%d)\n"
  109.          "-e file   file to log error messages to\n"
  110.          "-s file   file to spool backup syslogs to\n"
  111.          "-l file   file to log all debugging messages to\n\n",
  112.                  IPPORT_RESERVED-1);
  113.  
  114.    return;
  115. }
  116.  
  117.  
  118. /* ----------------------- */
  119.  
  120.  
  121. int main(int argc, char **argv)
  122. {
  123.    int res;           
  124.  
  125.    if (getuid() != 0) 
  126.    {
  127.       fprintf(stderr, "ERROR: This program must be run as root (uid 0). "
  128.                       "Aborting.\n\n");
  129.       exit(ERROR);
  130.    }
  131.  
  132.    memset(prevlogmsg, 0, sizeof(prevlogmsg));
  133.  
  134.    if (argv[1] != NULL)
  135.       (void)printf("%client ID: %s\n%c", (debugging != 1 ? 'C' : 'c'), ID,
  136.                    (strncmp(argv[1], "-h", 2) != 0 ? '\n' : '\0'));
  137.  
  138.    else
  139.       (void)printf("%client ID: %s\n\n", (debugging != 1 ? 'C' : 'c'), ID);
  140.  
  141.    getArgs(argc, argv);
  142.  
  143.    if (debugging == 1)
  144.    {
  145.       printf("\n<*> SRS by RSI <*>\n\n");
  146.  
  147.       printf("Warning: debugging outputs data quickly\n");
  148.       printf("Use %s -h for help on proper usage, and read SRS.doc.\n\n",
  149.              argv[0]);
  150.  
  151.       printf("Press 'Enter' (or 'Return') to continue\n");
  152.       (void)getchar();
  153.    }
  154.  
  155.    else
  156.    {
  157.       printf("SRS by RSI..\n");
  158.       printf("Use %s -h for help on proper usage, and read SRS.doc\n\n",
  159.              argv[0]);
  160.    }
  161.  
  162. #  ifndef SUN
  163.    system("killall syslogd &>/dev/null");
  164. #  endif
  165.  
  166.    createDirs(), setupFiles(), init(); 
  167.  
  168.    debug("local timezone: %s\n\n", tzname[0]);
  169.  
  170.    /* set up signals */
  171.    (void)signal(SIGHUP, SIG_IGN);
  172.  
  173.    (void)signal(SIGINT, sighandler);
  174.    (void)signal(SIGTERM, sighandler);
  175.  
  176.    (void)signal(SIGPIPE, SIG_IGN);
  177.  
  178.    (void)signal(SIGUSR1, SIG_IGN); 
  179.    (void)signal(SIGUSR2, SIG_IGN);
  180.    
  181.    if (debugging != 1)
  182.    {
  183.       /* we only want to be able to produce a core if debugging */
  184.       (void)signal(SIGQUIT, sighandler);
  185.  
  186.       /* we need these to make sure we exit properly */
  187.       (void)signal(SIGILL, sighandler);
  188.       (void)signal(SIGBUS, sighandler);
  189.       (void)signal(SIGSEGV, sighandler); 
  190.    }
  191.  
  192.    /* ------------- */
  193.    
  194.    debug("parsing %s... don't try to understand this\n", CONFILE);
  195.    debug("--------------------------------------------------------\n\n");
  196.  
  197.  
  198.    config(); /* handle config file */
  199.    (void)signal(SIGHUP, sighandler); /* re-handle the config file */ 
  200.  
  201.    initStream(); /* create /dev/log, setup unix socket, etc. */
  202.  
  203.    getSRSuser();
  204.  
  205. #  if !defined(SUN) && !defined(BSD)
  206.    if (pwd != NULL)
  207.    {
  208. #     ifdef _POSIX_SAVED_IDS
  209.       res = setuid(pwd->pw_uid);
  210. #     else
  211.       res = seteuid(pwd->pw_uid);
  212. #     endif
  213.  
  214.       if (res == ERROR)
  215.       {
  216.          error("error setting [e]uid: %s\n\n", strerror(errno));
  217.          quit(ERROR);
  218.       }
  219.    }
  220. #  endif
  221.  
  222.    /* ------------------------------ */
  223.  
  224.    (void)setjmp(infoconn);
  225.  
  226. #  if !defined(SUN) && !defined(BSD)
  227.    if (pwd != NULL)
  228.    {
  229. #     ifdef _POSIX_SAVED_IDS
  230.       res = setuid(pwd->pw_uid);
  231. #     else
  232.       res = seteuid(pwd->pw_uid);
  233. #     endif
  234.  
  235.       if (res == ERROR)
  236.       {
  237.          error("error setting [e]uid: %s\n\n", strerror(errno));
  238.          quit(ERROR);
  239.       }
  240.    }
  241. #  endif
  242.  
  243. InfoConn:
  244.    signal(SIGPIPE, SIG_IGN);
  245.  
  246. /*
  247.    if (connected == 1)
  248.    {
  249.       connected = 0; 
  250.  
  251.       debug("now disconnecting from server...\n");   
  252.       send_data("QUIT\n"), sleep(NORMPAUSE);
  253.  
  254.       res = close(sockfd);
  255.       if (res == ERROR)
  256.       {
  257.          error("error closing the socket: %s\n\n", strerror(errno));
  258.          quit(ERROR);
  259.       }  
  260.  
  261.       sockfd = ERROR;
  262.    }
  263.  
  264.    else
  265. */
  266.       (void)close(sockfd), sockfd = ERROR;
  267.  
  268.    gotInfoServ = 0;
  269.    errors = 0, connected = 0;
  270.  
  271.    (void)signal(SIGPIPE, dropinfoconn);
  272.  
  273.    initConn(PRIMSERV, SECSERV);  /* connect to info server */
  274.  
  275.    if ((errors == 1) && (connected != 1))
  276.    {
  277.       signal(SIGPIPE, SIG_IGN);
  278.  
  279.       if (spooling != 1)
  280.       {
  281.          error("error connecting to both info servers..\n"
  282.                "now starting spooling\n\n");
  283.  
  284.          silent = 1, forkSpool();
  285.          goto InfoConn;
  286.       }
  287.  
  288.       else
  289.       { 
  290.          (void)sleep(MAXPAUSE);
  291.          debug("(in spool parent) now restarting with info servers\n");
  292.  
  293.          goto InfoConn;
  294.       }
  295.    }
  296.  
  297.    else if ((connected == 1) && (spooling == 1))
  298.    {
  299.       silent = 0;
  300.       debug("(in spool parent) now connected to an info server..\n");
  301.       killSpooler();      
  302.    }
  303.  
  304.    (void)signal(SIGPIPE, dropinfoconn); /* for the info server    */
  305.    
  306.    (void)printf("%cow initializing, please hold..\n",
  307.                 (debugging != 1 ? 'N' : 'n'));
  308.  
  309.    (void)printf("%cttemping to authenticate with the info server...\n",
  310.                (debugging != 1 ? 'A' : 'a'));
  311.  
  312.    if (debugging == 1)
  313.    {
  314.       (void)putchar('\n');
  315.       (void)write(dblogfd, "\n", 1);
  316.    }
  317.  
  318.    send_data("ID %s", ID), doAuth();
  319.  
  320.    getVers();
  321.  
  322.    debug("now we have list of new client/server versions..\n"); 
  323.  
  324.    getServList();  /* get list of servers */
  325.    debug("now we have the streaming server list..\n"); 
  326.  
  327.    gotInfoServ = 1;
  328.  
  329.    /* ------------------------------ */
  330.  
  331.    (void)setjmp(reconnect);  /* longjmp() here to reconnect */
  332.    (void)signal(SIGPIPE, SIG_IGN);
  333.  
  334. #  if !defined(SUN) && !defined(BSD)
  335.    if (pwd != NULL)
  336.    {
  337. #     ifdef _POSIX_SAVED_IDS
  338.       res = setuid(pwd->pw_uid);
  339. #     else
  340.       res = seteuid(pwd->pw_uid);
  341. #     endif
  342.    
  343.       if (res == ERROR)
  344.       {
  345.          error("error setting [e]uid: %s\n\n", strerror(errno));
  346.          quit(ERROR);
  347.       }
  348.    }
  349. #  endif
  350.  
  351.    errors = 0;
  352.    nextServer(); /* get to next server (curServ changes) */
  353.  
  354.    /* FIX - still need to compare it to localhost */
  355.  
  356.    if ((errors == 1) && (initing == 1))
  357.    {
  358.       debug("info server and streaming server the same.. continuing\n");
  359.       goto init1;
  360.    }
  361.  
  362. /*
  363.    if (connected == 1)
  364.    {
  365.       connected = 0;
  366.       signal(SIGPIPE, SIG_IGN);
  367.  
  368.       debug("now disconnecting from server...\n");   
  369.       send_data("QUIT\n"), sleep(NORMPAUSE);
  370.  
  371.       res = close(sockfd);
  372.       if (res == ERROR)
  373.       {
  374.          error("error closing the socket: %s\n\n", strerror(errno));
  375.          quit(ERROR);
  376.       }  
  377.  
  378.       sockfd = ERROR;
  379.    }
  380.  
  381.    else
  382. */
  383.       (void)close(sockfd), sockfd = ERROR;
  384.  
  385.    if (child == 1) killPinger();
  386.  
  387.    if (sockfd > 0)
  388.    {
  389.       if (debugging == 1)
  390.       {
  391.          (void)putchar('\n');
  392.          (void)write(dblogfd, "\n", 1);
  393.       }
  394.    }
  395.  
  396.    else connected = 0;
  397.  
  398.    debug("now connecting to a streaming server...\n");
  399.  
  400.    /* ------------------------------ */
  401.  
  402.    /* when server drops conn or we're spooling.. we come here */
  403.  
  404. init1:
  405.    (void)setjmp(dropconn), (void)setjmp(dospool);
  406.  
  407.    errors = 0;
  408.  
  409. #  if !defined(SUN) && !defined(BSD)
  410.    if (pwd != NULL)
  411.    {
  412. #     ifdef _POSIX_SAVED_IDS
  413.       res = setuid(pwd->pw_uid);
  414. #     else
  415.       res = seteuid(pwd->pw_uid);
  416. #     endif
  417.  
  418.       if (res == ERROR)
  419.       {
  420.          error("error setting [e]uid: %s\n\n", strerror(errno));
  421.          quit(ERROR);
  422.       }
  423.    }
  424. #  endif
  425.  
  426.    if (initing == 1) goto startwork;
  427.  
  428.    (void)signal(SIGPIPE, SIG_IGN);          /* just to be safe */
  429.  
  430.    if (child == 1) killPinger();
  431.  
  432.    doServer();
  433.  
  434.    connected = 0;
  435.    res = connServ(servList[curServ].name); 
  436.    if (res == ERROR)
  437.    {
  438.       debug("(after connServ) error occured, restarting\n");
  439.       longjmp(dropconn, 1);
  440.    }
  441.  
  442.    (void)signal(SIGPIPE, sighandler);
  443.  
  444.    if (connected == 1)
  445.    {
  446.       silent = 0;
  447.       debug("(in parent) connected to a streaming server..\n");
  448.  
  449.       if (spooling == 1) killSpooler();
  450.    }
  451.  
  452.    else longjmp(dropconn, 1);
  453.  
  454.    (void)printf("%cuthenticating ourselves with the streaming server...\n\n",
  455.                (debugging != 1 ? 'A' : 'a'));
  456.  
  457.    send_data("ID %s", ID), doAuth();
  458.  
  459. startwork:
  460.    if (initing == 1) initing = 0;
  461.  
  462.    (void)signal(SIGPIPE, sighandler);
  463.  
  464.    if (debugging != 1) daemonize();
  465.    else (void)printf("running in debug mode... not forking/daemonizing\n");
  466.  
  467.    start = 0, running = 1;
  468.  
  469.    if (debugging == 1)
  470.    {
  471.       (void)putchar('\n');
  472.       (void)write(dblogfd, "\n", 1);
  473.    }
  474.  
  475.    /* getSubID(); */ /* do we really need it? */
  476.  
  477.    while(1) procData();
  478.    return SUCCESS;
  479. }
  480.